const express = require('express');
const connection = require('../mysql') // 获取连接实例
const bcrypt = require('bcryptjs');
const vertoken = require('../public/token')
const app = express();
const $sql = require('../db');

app.get('/login_em_ac', (req, res) => {
  connection.query($sql.login_sql.login_em_ac,(err, users) => {
    if (err) {
      return res.send(err);
    } else {
      return res.send(users);
    }
  });
});
app.post('/login_em_ps', (req, res) => {
  var params = req.body;
  connection.query($sql.login_sql.login_em_ps,[params.account],(err, users) => {
    if (err) {
      res.send(err);
    } else {
		 if(bcrypt.compareSync(params.password,users[0].password)){
			connection.query($sql.login_sql.login_de_id,[params.account],(err,deid) => {
				vertoken.setToken(params.account,"user").then((data)=>{
				    return res.json({ token: data,code:200,message:'登录成功',deid:deid});
				})
			})
		 }else{
		  return res.sendStatus(404);
	  }
    }
  });
});
app.get('/login_sp_ac', (req, res) => {
  var params = req.body;
  connection.query($sql.login_sql.login_sp_ac,(err, users) => {
   if (err) {
      return res.send(err);
    } else {
      return res.send(users);
    }
  });
});
app.post('/login_sp_ps', (req, res) => {
  var params = req.body;
  connection.query($sql.login_sql.login_sp_ps,[params.account],(err, users) => {
    if (err) {
      return res.send(err);
    } else {
	  if(bcrypt.compareSync(params.password,users[0].password)){
		  vertoken.setToken(params.account,"super").then((data)=>{
		      return res.json({ token: data,code:200,message:'登录成功' });
		  })
	  }else{
		  return res.send(404);
	  }
    }
  });
});

module.exports = app;